/*
 * @Author: guojinxin_hub 1907745233@qq.com
 * @Date: 2024-09-18 20:20:42
 * @LastEditors: guojinxin_hub 1907745233@qq.com
 * @LastEditTime: 2024-09-18 21:19:07
 * @FilePath: \qintai-service\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { Controller } = require('egg');
const fs = require('fs');
const path = require('path');
class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    // const options = {
    //   method: 'GET',
    //   data: {
    //     projectId: 8033,
    //     calObject: '秦台水库',
    //     dataType: '2',
    //     calSchemeId: 'QTSK-3-50',
    //     dtid: '12',
    //     isGetZ: true,
    //     IsGetUV: true,
    //   },
    //   // contentType: 'json',
    //   dataType: 'json',
    // };
    // const requestRes = await ctx.curl(
    //   'http://116.204.5.168:2108/JHHC/Hydraulics/Calculate/GetHydraulics2DNetData',
    //   options
    // );
    // console.log(2222222222, requestRes.data.data);
    for (let index = 143; index < 145; index++) {


      const requestRes = await ctx.curl(`http://116.204.5.168:2108/JHHC/Hydraulics/Calculate/GetHydraulics2DNetData?projectId=8083&calObject=秦台水库&dataType=2&calSchemeId=QTSK-3-50&dtid=${index}&hasElementId=true&isGetH=true&isGetZ=true&IsGetUV=true`, { dataType: 'json' });
      const triangles = requestRes.data;

      const dataDict = {};

      triangles.data.forEach(dataType => {

        dataType.dataValues.forEach(dataValue => {

          const idValue = dataValue.id;
          if (!dataDict[idValue]) {
            dataDict[idValue] = {
            };
          }
          dataDict[idValue][dataType.dataType] = dataValue.value;
        });
      });

      const result = [];
      for (const idValue in dataDict) {
        const combinedData = { id: parseInt(idValue) + 1 };
        const data = {
          id: combinedData.id,
          h: dataDict[idValue]['水深'] ? dataDict[idValue]['水深'] : 0,
          u: dataDict[idValue]['流速U'] ? dataDict[idValue]['流速U'] : 0,
          v: dataDict[idValue]['流速V'] ? dataDict[idValue]['流速V'] : 0,
        };

        result.push(data);
      }

      // console.log(result);


      // for (let i = 0; i < all.length; i++) {
      const datFilePath = path.join('D:\\TEST', `TEST_${index}.dat`);
      // const jsFilePath = path.join('D:\\TEST', `TEST_${index}.js`);
      const datStream = fs.createWriteStream(datFilePath);
      // const jsStream = fs.createWriteStream(jsFilePath);
      const jsContent = [];
      // 检查文件路径是否正确
      // console.log('Dat file path:', datFilePath);
      // console.log('JS file path:', jsFilePath);
      for (let i = 0; i < result.length; i++) {
      // console.log(22222222, result[i]);
        const id = result[i].id;
        const h = result[i].h;
        const u = result[i].u;
        const v = result[i].v;

        // 写入 JS 文件内容a
        jsContent.push(`id:${id}h:${h}u:${u}v:${v}`);

        // 检查缓冲区大小的计算
        const bufferSize = 4 + 8 + 8 + 8;
        // console.log('Buffer size:', bufferSize);

        const buffer = Buffer.alloc(bufferSize);
        // console.log('[ id ] >', id)
        buffer.writeIntLE(id, 0, 4);
        // console.log('[ h ] >', h)
        buffer.writeDoubleLE(h, 4);
        // console.log('[ u ] >', u)
        buffer.writeDoubleLE(u, 12);
        // console.log('[ v ] >', v)
        buffer.writeDoubleLE(v, 20);

        // 检查写入的数据是否正确
        // console.log('Writing buffer:', buffer);
        datStream.write(buffer);
      }
      // jsStream.write(jsContent.join('\n'));

      datStream.end();
      // jsStream.end();
    }
    ctx.body = 'hi, egg';
  }
}

module.exports = HomeController;
